package guide;

import org.junit.Test;

import java.util.LinkedList;

/**
 * @author fance
 * @date 18-5-3 上午11:40
 */
public class StackAndQueue {

    /**
     * 用一个栈给另一个栈排序
     * @param stack
     */
    public void sortStackByStack(LinkedList<Integer> stack) {
        if (stack == null) {
            return;
        }
        LinkedList<Integer> helpStack = new LinkedList<>();
        while (!stack.isEmpty()) {
            int cur = stack.pop();
            if (!helpStack.isEmpty() && cur < helpStack.peek()){
                stack.push(helpStack.pop());
            }
            helpStack.push(cur);
        }
        while (!helpStack.isEmpty()) {
            stack.push(helpStack.pop());
        }
    }

    @Test
    public void test() {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(4);
        stack.push(2);
        stack.push(1);
        stack.push(3);
        sortStackByStack(stack);
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
